package com.shinhye.mobilelottery.net;

import org.apache.commons.lang3.StringUtils;

import com.shinhye.mobilelottery.GlobalParams;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Proxy;

public class NetUtils {
	/**
	 * Specify the network type is WIFI.
	 */
	public static int TYPE_WIFI = ConnectivityManager.TYPE_WIFI;
	/**
	 * Specify the network type is MOBILE NETWORK.
	 */
	public static int TYPE_MOBILE = ConnectivityManager.TYPE_MOBILE;

	/**
	 * Check for network if there is network available.
	 * @param context
	 * @return If there is no network, return false, otherwise, return true.
	 */
	public static boolean checkNetWork(Context context) {
		boolean isWifi = isNetConnected(context, TYPE_WIFI);
		boolean isMobile = isNetConnected(context, TYPE_MOBILE);
		if(!isWifi && !isMobile) {
			return false;
		}
		
		if(isMobile) {
			String ip = Proxy.getDefaultHost();
			if(StringUtils.isNotBlank(ip)) {
				//有代理ip说明手机正通过基站wap方式上网
				GlobalParams.isWap = true;
			}
		}
		
		return true;
	}
	
	/**
	 * Judge if a type of network has been connected.
	 * @param context
	 * @param type Typically, use {@link #TYPE_WIFI} to specified a WIFI NetWork, and use 
	 * {@link #TYPE_MOBILE} to specified a MOBILE NetWork.
	 */
	public static boolean isNetConnected(Context context, int type) {
		ConnectivityManager conn_service = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = conn_service.getNetworkInfo(type);
		if(info != null) {
			return info.isConnected();
		}
		return false;
	}
}
